package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 品牌的分页查询
     * @param page
     * @param rows
//     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>>pageQuery(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                      @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                      @RequestParam(value = "key",required = false)String key){
        return ResponseEntity.ok(this.brandService.pageQuery(page,rows,key));
    }


    /**
     * 根据品牌id查询对应的品牌对象
     * @param id
     * @return
     */
        @GetMapping("/{id}")
    public ResponseEntity<BrandDTO>queryBrandById(@PathVariable("id")Long id){
            return ResponseEntity.ok(this.brandService.queryBrandById(id));

        }

    /**
     * 根据品牌id的集合查询对应的品牌集合
     * @param ids
     * @return
     */
@GetMapping("/list")
    public ResponseEntity<List<BrandDTO>>listBrandByIds(@RequestParam("ids")List<Long>ids){
    return ResponseEntity.ok(this.brandService.listBrandByIds(ids));

}

    /**
     * 根据分类查品牌
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>>queryBrandByCategory(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.brandService.queryBrandByCategory(cid));

    }


    /**
     * 品牌新增业务
     * @param name
     * @param letter
     * @param image
     * @param categoryIds
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>addBrand(@RequestParam("name") String name,
                                        @RequestParam("letter") Character letter,
                                        @RequestParam(value = "image",required = false)String image,
                                        @RequestParam("categoryIds") List<Long> categoryIds){
        this.brandService.addBrand(name,letter,image,categoryIds);
//        新增品牌处理
       return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 品牌修改业务
     * @param name
     * @param letter
     * @param image
     * @param categoryIds
     * @return
     */
@PutMapping
    public ResponseEntity<Void>updateBrand( @RequestParam("id")Long id,
                                            @RequestParam("name") String name,
                                             @RequestParam("letter") Character letter,
                                            @RequestParam(value = "image",required = false)String image,
                                              @RequestParam("categoryIds") List<Long> categoryIds){
        this.brandService.updateBrand(id,name,letter,image,categoryIds);
//        新增品牌处理
        return ResponseEntity.ok().build();
}
    /**
     * 根据品牌id删除对应的品牌信息
     * @param bid
     * @return
     */

    @DeleteMapping("/{id}")
     public ResponseEntity<Void>deleteBrandById(@PathVariable("id") Long bid){
        this.brandService.deleteBrandById(bid);
        return ResponseEntity.ok().build();

    }


}